package com.example.zwgt.yrfk.ui.access.activity;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.zwgt.yrfk.R;
import com.example.zwgt.yrfk.base.mvpbase.BasePresenter;
import com.example.zwgt.yrfk.base.mvpbase.baseImpl.BaseActivity;
import com.example.zwgt.yrfk.ui.risk.adapter.FullyGridLayoutManager;
import com.example.zwgt.yrfk.ui.risk.adapter.GridImageAdapter;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.luck.picture.lib.config.PictureMimeType;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class NextAddUserActivity extends BaseActivity {


    @BindView(R.id.rvimg)
    RecyclerView rvImg;
    @BindView(R.id.iv_back)
    ImageView ivBack;
    @BindView(R.id.tv_save)
    TextView tvSave;
    @BindView(R.id.rl_top)
    RelativeLayout rlTop;
    @BindView(R.id.tv_idcard)
    TextView tvIdcard;
    @BindView(R.id.tv_certificate)
    TextView tvCertificate;
    @BindView(R.id.rv_img1)
    RecyclerView rvImg1;
    @BindView(R.id.tv_certificate_location)
    TextView tvCertificateLocation;
    @BindView(R.id.tv_togetherphoto)
    TextView tvTogetherphoto;
    @BindView(R.id.rv_img2)
    RecyclerView rvImg2;
    @BindView(R.id.tv_togetherphoto_location)
    TextView tvTogetherphotoLocation;
    @BindView(R.id.rv_img3)
    RecyclerView rvImg3;
    @BindView(R.id.tv_signpic_location)
    TextView tvSignpicLocation;
    @BindView(R.id.rv_img4)
    RecyclerView rvImg4;
    @BindView(R.id.rv_img5)
    RecyclerView rvImg5;
    @BindView(R.id.rv_img6)
    RecyclerView rvImg6;
    @BindView(R.id.rv_img7)
    RecyclerView rvImg7;
    @BindView(R.id.rv_img8)
    RecyclerView rvImg8;
    private GridImageAdapter adapter,adapter1,adapter2,adapter3,adapter4,adapter5,adapter6,adapter7,adapter8;
    private List<LocalMedia> selectList = new ArrayList<>();
    private List<LocalMedia> selectList1 = new ArrayList<>();
    private List<LocalMedia> selectList2 = new ArrayList<>();
    private List<LocalMedia> selectList3 = new ArrayList<>();
    private List<LocalMedia> selectList4 = new ArrayList<>();
    private List<LocalMedia> selectList5 = new ArrayList<>();
    private List<LocalMedia> selectList6 = new ArrayList<>();
    private List<LocalMedia> selectList7 = new ArrayList<>();
    private List<LocalMedia> selectList8 = new ArrayList<>();


    private int selectSum = 2;

    @Override
    public int getLayoutId() {
        return R.layout.add_next;
    }

    /**
     * 入口
     *
     * @param context
     */
    public static void startAction(Context context) {
        Intent intent = new Intent(context, NextAddUserActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void initViews() {
        FullyGridLayoutManager manager = new FullyGridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false);
        FullyGridLayoutManager manager1 = new FullyGridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false);
        FullyGridLayoutManager manager2 = new FullyGridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false);
        FullyGridLayoutManager manager3 = new FullyGridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false);
        FullyGridLayoutManager manager4 = new FullyGridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false);
        FullyGridLayoutManager manager5 = new FullyGridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false);
        FullyGridLayoutManager manager6 = new FullyGridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false);
        FullyGridLayoutManager manager7 = new FullyGridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false);
        FullyGridLayoutManager manager8 = new FullyGridLayoutManager(this, 3, GridLayoutManager.VERTICAL, false);

        rvImg.setLayoutManager(manager);
        rvImg1.setLayoutManager(manager1);
        rvImg2.setLayoutManager(manager2);
        rvImg3.setLayoutManager(manager3);
        rvImg4.setLayoutManager(manager4);
        rvImg5.setLayoutManager(manager5);
        rvImg6.setLayoutManager(manager6);
        rvImg7.setLayoutManager(manager7);
        rvImg8.setLayoutManager(manager8);
        adapter = new GridImageAdapter(this, onAddPicClick0);
        adapter1 = new GridImageAdapter(this, onAddPicClick1);
        adapter2 = new GridImageAdapter(this, onAddPicClick2);
        adapter3 = new GridImageAdapter(this, onAddPicClick3);
        adapter4 = new GridImageAdapter(this, onAddPicClick4);
        adapter5 = new GridImageAdapter(this, onAddPicClick5);
        adapter6 = new GridImageAdapter(this, onAddPicClick6);
        adapter7 = new GridImageAdapter(this, onAddPicClick7);
        adapter8 = new GridImageAdapter(this, onAddPicClick8);
        adapter.setList(selectList);
        adapter1.setList(selectList1);
        adapter2.setList(selectList2);
        adapter3.setList(selectList3);
        adapter4.setList(selectList4);
        adapter5.setList(selectList5);
        adapter6.setList(selectList6);
        adapter7.setList(selectList7);
        adapter8.setList(selectList8);


        adapter.setSelectMax(2);
        adapter1.setSelectMax(2);
        adapter2.setSelectMax(2);
        adapter3.setSelectMax(4);
        adapter4.setSelectMax(4);
        adapter5.setSelectMax(2);
        adapter6.setSelectMax(4);
        adapter7.setSelectMax(2);
        adapter8.setSelectMax(9);

        rvImg.setAdapter(adapter);
        rvImg1.setAdapter(adapter1);
        rvImg2.setAdapter(adapter2);
        rvImg3.setAdapter(adapter3);
        rvImg4.setAdapter(adapter4);
        rvImg5.setAdapter(adapter5);
        rvImg6.setAdapter(adapter6);
        rvImg7.setAdapter(adapter7);
        rvImg8.setAdapter(adapter8);

        adapter.setOnItemClickListener(new GridImageAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                if (selectList.size() > 0) {
                    LocalMedia media = selectList.get(position);
                    String pictureType = media.getPictureType();
                    int mediaType = PictureMimeType.pictureToVideo(pictureType);
                    switch (mediaType) {
                        case 1:
                            // 预览图片 可自定长按保存路径
                            //PictureSelector.create(MainActivity.this).externalPicturePreview(position, "/custom_file", selectList);
                            PictureSelector.create(NextAddUserActivity.this).externalPicturePreview(position, selectList);
                            break;
                        case 2:
                            // 预览视频
                            PictureSelector.create(NextAddUserActivity.this).externalPictureVideo(media.getPath());
                            break;
                        case 3:
                            // 预览音频
                            PictureSelector.create(NextAddUserActivity.this).externalPictureAudio(media.getPath());
                            break;
                    }
                }
            }
        });
        adapter1.setOnItemClickListener(new GridImageAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                if (selectList.size() > 0) {
                    LocalMedia media = selectList.get(position);
                    String pictureType = media.getPictureType();
                    int mediaType = PictureMimeType.pictureToVideo(pictureType);
                    switch (mediaType) {
                        case 1:
                            // 预览图片 可自定长按保存路径
                            //PictureSelector.create(MainActivity.this).externalPicturePreview(position, "/custom_file", selectList);
                            PictureSelector.create(NextAddUserActivity.this).externalPicturePreview(position, selectList1);
                            break;
                        case 2:
                            // 预览视频
                            PictureSelector.create(NextAddUserActivity.this).externalPictureVideo(media.getPath());
                            break;
                        case 3:
                            // 预览音频
                            PictureSelector.create(NextAddUserActivity.this).externalPictureAudio(media.getPath());
                            break;
                    }
                }
            }
        });

        adapter2.setOnItemClickListener(new GridImageAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                if (selectList.size() > 0) {
                    LocalMedia media = selectList.get(position);
                    String pictureType = media.getPictureType();
                    int mediaType = PictureMimeType.pictureToVideo(pictureType);
                    switch (mediaType) {
                        case 1:
                            // 预览图片 可自定长按保存路径
                            //PictureSelector.create(MainActivity.this).externalPicturePreview(position, "/custom_file", selectList);
                            PictureSelector.create(NextAddUserActivity.this).externalPicturePreview(position, selectList2);
                            break;
                        case 2:
                            // 预览视频
                            PictureSelector.create(NextAddUserActivity.this).externalPictureVideo(media.getPath());
                            break;
                        case 3:
                            // 预览音频
                            PictureSelector.create(NextAddUserActivity.this).externalPictureAudio(media.getPath());
                            break;
                    }
                }
            }
        });
        adapter3.setOnItemClickListener(new GridImageAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                if (selectList.size() > 0) {
                    LocalMedia media = selectList.get(position);
                    String pictureType = media.getPictureType();
                    int mediaType = PictureMimeType.pictureToVideo(pictureType);
                    switch (mediaType) {
                        case 1:
                            // 预览图片 可自定长按保存路径
                            //PictureSelector.create(MainActivity.this).externalPicturePreview(position, "/custom_file", selectList);
                            PictureSelector.create(NextAddUserActivity.this).externalPicturePreview(position, selectList3);
                            break;
                        case 2:
                            // 预览视频
                            PictureSelector.create(NextAddUserActivity.this).externalPictureVideo(media.getPath());
                            break;
                        case 3:
                            // 预览音频
                            PictureSelector.create(NextAddUserActivity.this).externalPictureAudio(media.getPath());
                            break;
                    }
                }
            }
        });
        adapter4.setOnItemClickListener(new GridImageAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                if (selectList.size() > 0) {
                    LocalMedia media = selectList.get(position);
                    String pictureType = media.getPictureType();
                    int mediaType = PictureMimeType.pictureToVideo(pictureType);
                    switch (mediaType) {
                        case 1:
                            // 预览图片 可自定长按保存路径
                            //PictureSelector.create(MainActivity.this).externalPicturePreview(position, "/custom_file", selectList);
                            PictureSelector.create(NextAddUserActivity.this).externalPicturePreview(position, selectList4);
                            break;
                        case 2:
                            // 预览视频
                            PictureSelector.create(NextAddUserActivity.this).externalPictureVideo(media.getPath());
                            break;
                        case 3:
                            // 预览音频
                            PictureSelector.create(NextAddUserActivity.this).externalPictureAudio(media.getPath());
                            break;
                    }
                }
            }
        });
        adapter5.setOnItemClickListener(new GridImageAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                if (selectList.size() > 0) {
                    LocalMedia media = selectList.get(position);
                    String pictureType = media.getPictureType();
                    int mediaType = PictureMimeType.pictureToVideo(pictureType);
                    switch (mediaType) {
                        case 1:
                            // 预览图片 可自定长按保存路径
                            //PictureSelector.create(MainActivity.this).externalPicturePreview(position, "/custom_file", selectList);
                            PictureSelector.create(NextAddUserActivity.this).externalPicturePreview(position, selectList5);
                            break;
                        case 2:
                            // 预览视频
                            PictureSelector.create(NextAddUserActivity.this).externalPictureVideo(media.getPath());
                            break;
                        case 3:
                            // 预览音频
                            PictureSelector.create(NextAddUserActivity.this).externalPictureAudio(media.getPath());
                            break;
                    }
                }
            }
        });
        adapter6.setOnItemClickListener(new GridImageAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                if (selectList.size() > 0) {
                    LocalMedia media = selectList.get(position);
                    String pictureType = media.getPictureType();
                    int mediaType = PictureMimeType.pictureToVideo(pictureType);
                    switch (mediaType) {
                        case 1:
                            // 预览图片 可自定长按保存路径
                            //PictureSelector.create(MainActivity.this).externalPicturePreview(position, "/custom_file", selectList);
                            PictureSelector.create(NextAddUserActivity.this).externalPicturePreview(position, selectList6);
                            break;
                        case 2:
                            // 预览视频
                            PictureSelector.create(NextAddUserActivity.this).externalPictureVideo(media.getPath());
                            break;
                        case 3:
                            // 预览音频
                            PictureSelector.create(NextAddUserActivity.this).externalPictureAudio(media.getPath());
                            break;
                    }
                }
            }
        });
        adapter7.setOnItemClickListener(new GridImageAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                if (selectList.size() > 0) {
                    LocalMedia media = selectList.get(position);
                    String pictureType = media.getPictureType();
                    int mediaType = PictureMimeType.pictureToVideo(pictureType);
                    switch (mediaType) {
                        case 1:
                            // 预览图片 可自定长按保存路径
                            //PictureSelector.create(MainActivity.this).externalPicturePreview(position, "/custom_file", selectList);
                            PictureSelector.create(NextAddUserActivity.this).externalPicturePreview(position, selectList7);
                            break;
                        case 2:
                            // 预览视频
                            PictureSelector.create(NextAddUserActivity.this).externalPictureVideo(media.getPath());
                            break;
                        case 3:
                            // 预览音频
                            PictureSelector.create(NextAddUserActivity.this).externalPictureAudio(media.getPath());
                            break;
                    }
                }
            }
        });
        adapter8.setOnItemClickListener(new GridImageAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position, View v) {
                if (selectList.size() > 0) {
                    LocalMedia media = selectList.get(position);
                    String pictureType = media.getPictureType();
                    int mediaType = PictureMimeType.pictureToVideo(pictureType);
                    switch (mediaType) {
                        case 1:
                            // 预览图片 可自定长按保存路径
                            //PictureSelector.create(MainActivity.this).externalPicturePreview(position, "/custom_file", selectList);
                            PictureSelector.create(NextAddUserActivity.this).externalPicturePreview(position, selectList8);
                            break;
                        case 2:
                            // 预览视频
                            PictureSelector.create(NextAddUserActivity.this).externalPictureVideo(media.getPath());
                            break;
                        case 3:
                            // 预览音频
                            PictureSelector.create(NextAddUserActivity.this).externalPictureAudio(media.getPath());
                            break;
                    }
                }
            }
        });

    }

    @Override
    public BasePresenter initPresenter() {
        return null;
    }

    @OnClick({R.id.iv_back, R.id.tv_save})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
            case R.id.tv_save:
                //showPop();
                break;

        }
    }


    private GridImageAdapter.onAddPicClickListener onAddPicClick0 = new GridImageAdapter.onAddPicClickListener() {
        @Override
        public void onAddPicClick() {
            PictureSelector.create(NextAddUserActivity.this)
                    .openGallery(PictureMimeType.ofImage())
                    .maxSelectNum(2)
                    .minSelectNum(1)
                    .imageSpanCount(4)
                    .selectionMode(PictureConfig.MULTIPLE)
                    .forResult(0);
        }
    };
    private GridImageAdapter.onAddPicClickListener onAddPicClick1 = new GridImageAdapter.onAddPicClickListener() {
        @Override
        public void onAddPicClick() {
            PictureSelector.create(NextAddUserActivity.this)
                    .openGallery(PictureMimeType.ofImage())
                    .maxSelectNum(2)
                    .minSelectNum(1)
                    .imageSpanCount(4)
                    .selectionMode(PictureConfig.MULTIPLE)
                    .forResult(1);
        }
    };
    private GridImageAdapter.onAddPicClickListener onAddPicClick2 = new GridImageAdapter.onAddPicClickListener() {
        @Override
        public void onAddPicClick() {
            PictureSelector.create(NextAddUserActivity.this)
                    .openGallery(PictureMimeType.ofImage())
                    .maxSelectNum(4)
                    .minSelectNum(1)
                    .imageSpanCount(4)
                    .selectionMode(PictureConfig.MULTIPLE)
                    .forResult(2);
        }
    };
    private GridImageAdapter.onAddPicClickListener onAddPicClick3 = new GridImageAdapter.onAddPicClickListener() {
        @Override
        public void onAddPicClick() {
            PictureSelector.create(NextAddUserActivity.this)
                    .openGallery(PictureMimeType.ofImage())
                    .maxSelectNum(4)
                    .minSelectNum(1)
                    .imageSpanCount(4)
                    .selectionMode(PictureConfig.MULTIPLE)
                    .forResult(3);
        }
    };
    private GridImageAdapter.onAddPicClickListener onAddPicClick4 = new GridImageAdapter.onAddPicClickListener() {
        @Override
        public void onAddPicClick() {
            PictureSelector.create(NextAddUserActivity.this)
                    .openGallery(PictureMimeType.ofImage())
                    .maxSelectNum(4)
                    .minSelectNum(1)
                    .imageSpanCount(4)
                    .selectionMode(PictureConfig.MULTIPLE)
                    .forResult(4);
        }
    };
    private GridImageAdapter.onAddPicClickListener onAddPicClick5 = new GridImageAdapter.onAddPicClickListener() {
        @Override
        public void onAddPicClick() {
            PictureSelector.create(NextAddUserActivity.this)
                    .openGallery(PictureMimeType.ofImage())
                    .maxSelectNum(4)
                    .minSelectNum(1)
                    .imageSpanCount(4)
                    .selectionMode(PictureConfig.MULTIPLE)
                    .forResult(5);
        }
    };
    private GridImageAdapter.onAddPicClickListener onAddPicClick6 = new GridImageAdapter.onAddPicClickListener() {
        @Override
        public void onAddPicClick() {
            PictureSelector.create(NextAddUserActivity.this)
                    .openGallery(PictureMimeType.ofImage())
                    .maxSelectNum(4)
                    .minSelectNum(1)
                    .imageSpanCount(4)
                    .selectionMode(PictureConfig.MULTIPLE)
                    .forResult(6);
        }
    };
    private GridImageAdapter.onAddPicClickListener onAddPicClick7 = new GridImageAdapter.onAddPicClickListener() {
        @Override
        public void onAddPicClick() {
            PictureSelector.create(NextAddUserActivity.this)
                    .openGallery(PictureMimeType.ofImage())
                    .maxSelectNum(2)
                    .minSelectNum(1)
                    .imageSpanCount(4)
                    .selectionMode(PictureConfig.MULTIPLE)
                    .forResult(7);
        }
    };
    private GridImageAdapter.onAddPicClickListener onAddPicClick8 = new GridImageAdapter.onAddPicClickListener() {
        @Override
        public void onAddPicClick() {
            PictureSelector.create(NextAddUserActivity.this)
                    .openGallery(PictureMimeType.ofImage())
                    .maxSelectNum(9)
                    .minSelectNum(1)
                    .imageSpanCount(4)
                    .selectionMode(PictureConfig.MULTIPLE)
                    .forResult(8);
        }
    };



    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        List<LocalMedia> images;
        if (resultCode == RESULT_OK) {
            switch (requestCode) {
                case 0:
                    // 图片选择结果回调
                    images = PictureSelector.obtainMultipleResult(data);
                    selectList.addAll(images);
//                    selectList = PictureSelector.obtainMultipleResult(data);
                    // 例如 LocalMedia 里面返回三种path
                    // 1.media.getPath(); 为原图path
                    // 2.media.getCutPath();为裁剪后path，需判断media.isCut();是否为true
                    // 3.media.getCompressPath();为压缩后path，需判断media.isCompressed();是否为true
                    // 如果裁剪并压缩了，以取压缩路径为准，因为是先裁剪后压缩的
                    adapter.setList(selectList);
                    adapter.notifyDataSetChanged();
                    break;
                case 1:
                    images = PictureSelector.obtainMultipleResult(data);
                    selectList1.addAll(images);
                    adapter1.setList(selectList1);
                    adapter1.notifyDataSetChanged();
                    break;
                case 2:
                    images = PictureSelector.obtainMultipleResult(data);
                    selectList2.addAll(images);
                    adapter2.setList(selectList2);
                    adapter2.notifyDataSetChanged();
                    break;
                case 3:
                    images = PictureSelector.obtainMultipleResult(data);
                    selectList3.addAll(images);
                    adapter3.setList(selectList3);
                    adapter3.notifyDataSetChanged();
                    break;
                case 4:
                    images = PictureSelector.obtainMultipleResult(data);
                    selectList4.addAll(images);
                    adapter4.setList(selectList4);
                    adapter4.notifyDataSetChanged();
                    break;
                case 5:
                    images = PictureSelector.obtainMultipleResult(data);
                    selectList5.addAll(images);
                    adapter5.setList(selectList5);
                    adapter5.notifyDataSetChanged();
                    break;
                case 6:
                    images = PictureSelector.obtainMultipleResult(data);
                    selectList6.addAll(images);
                    adapter6.setList(selectList6);
                    adapter6.notifyDataSetChanged();
                    break;
                case 7:
                    images = PictureSelector.obtainMultipleResult(data);
                    selectList7.addAll(images);
                    adapter7.setList(selectList7);
                    adapter7.notifyDataSetChanged();
                    break;
                case 8:
                    images = PictureSelector.obtainMultipleResult(data);
                    selectList8.addAll(images);
                    adapter8.setList(selectList8);
                    adapter8.notifyDataSetChanged();
                    break;

            }
        }
    }

}
